/**
 */

package com.bei_en.teacher.admin.aop;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import com.bei_en.teacher.admin.controller.BaseController;
import com.bei_en.teacher.admin.model.users.vo.UsersVo;
import com.bei_en.teacher.admin.other.Consts;
import com.bei_en.common.util.Mylog;
import com.bei_en.common.web.JsonFailResult;
import com.bei_en.common.web.MySessionContext;

/**
 * @author Guocg
 *
 */
@Aspect
public class MylogAspect {

    public static Mylog log = new Mylog(MylogAspect.class);

    // (..) (*) (*,String)
    // @Pointcut("execution(* com1..*(..))")

    @Pointcut("execution(public * com.bei_en.teacher.admin.controller..*(..))"
	    +"&&!execution(public * com.bei_en.teacher.admin.controller.BaseController.*(..))"
	    +"&&!execution(public * com.bei_en.teacher.admin.controller..queryPayed(..))" +"&&execution(public * com.bei_en.teacher.admin.controller..StartWorkJob(..))")
    void pointcut() {

    }


    private HttpServletRequest request;

    private void setRequest(HttpServletRequest request) {

	if (this.request!=request) {
	    this.request = request;
	    log.setRequest(request);
	}
    }

    @After("pointcut()")
    public void After(JoinPoint jp) {

	HttpServletRequest request = ((BaseController) jp.getTarget()).getRequest();
	setRequest(request);
	log.warn("Ending: "+jp.getTarget().getClass().getName()+"."+jp.getSignature().getName());
    }

    // HttpRequest,HttpSession,HttpResponse,Cookie,Model,ModelAndView
    @Before("pointcut()")
    public void Before(JoinPoint jp) {
     	System.out.println("Before");
	HttpServletRequest request = ((BaseController) jp.getTarget()).getRequest();
	setRequest(request);

	log.warn("Begining: "+jp.getTarget().getClass().getName()+"."+jp.getSignature().getName());
    }

    @Around("pointcut()")
    public Object Around(ProceedingJoinPoint pjp) throws Throwable {

	HttpServletRequest request = ((BaseController) pjp.getTarget()).getRequest();
	setRequest(request);
	long time = System.currentTimeMillis();
	Object retVal = pjp.proceed();
	time = System.currentTimeMillis()-time;
	log.warn("process time: "+time+" ms");
	return retVal;
    }

    @AfterReturning(pointcut = "pointcut()", returning = "returning")
    public void AfterReturning(JoinPoint jp, Object returning) {
     	System.out.println("@AfterReturning");
	HttpServletRequest request = ((BaseController) jp.getTarget()).getRequest();
	setRequest(request);
	log.warn("returned: "+jp.getTarget().getClass().getName()+"."+jp.getSignature().getName());
	log.warn(returning);
    }

    @AfterThrowing(pointcut = "pointcut()", throwing = "throwing")
    public void AfterThrowing(JoinPoint jp, Exception throwing) {

	BaseController controller = (BaseController) jp.getTarget();
	HttpServletRequest request = controller.getRequest();
	setRequest(request);
	log.warn("throwed: "+jp.getTarget().getClass().getName()+"."+jp.getSignature().getName());
	log.warn(throwing);
    }


}
